<?php


namespace app\admin\controller\sertec;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="服务技师列表")
 */
class ServiceTechnician extends AdminController
{

    use \app\admin\traits\Curd;
    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\ServiceTechnician();
        
    }
    
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->where("auditing_status",1)
                ->count();
            $list = $this->model
                ->where($where)
                ->where("auditing_status",1)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }


    /**
    * @NodeAnotation(title="添加")
    */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['label_id'])){
                $this->error('标签不能为空');
            }
            if(empty($post['service_name'])){
                $this->error('名称不能为空');
            }
            if(empty($post['expert_group_id'])){
                $this->error('分组不能为空');
            }
            if(empty($post['image'])){
                $this->error('头像不能为空');
            }
            $flag = Db::name("service_technician")->where("phone",$post["phone"])->find();
            if(!empty($flag)){
                $this->error('手机号已存在,请重新输入');
            }
            $label_id = implode(",",$post["label_id"]);
            $data = [
                'expert_group_id'=>$post['expert_group_id'],
                'service_name'=>$post['service_name'],
                'label_id'=>$label_id,
                'service_img'=>$post['image'],
                'lun_img'=>$post['logo'],
                'phone'=>$post['phone'],
                'sex'=>$post['sex'],
                'age'=>$post['age'],
                'brief_introduction'=>$post['brief_introduction'],
                'password'=>md5("123456"),
                "auditing_status"=>1,
                'proficient_in'=>$post['proficient_in'],
                'create_time'=>time()
            ];
            try {
                $save = $this->model->save($data);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }else{
            $expert_group =  Db::name("expert_group")->order("id desc")->select();
            $this->assign("expert_group",$expert_group);

            $label_nan = Db::name("label")->where("sex",1)->select();
            $label_nv = Db::name("label")->where("sex",2)->select();
            $this->assign("label_nan",$label_nan);
            $this->assign("label_nv",$label_nv);
            return $this->fetch();
        }
        
    }

     /**
    * @NodeAnotation(title="编辑")
    */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['label_id'])){
                $this->error('标签不能为空');
            }
            if(empty($post['service_name'])){
                $this->error('名称不能为空');
            }
            if(empty($post['expert_group_id'])){
                $this->error('分组不能为空');
            }
            if(empty($post['image'])){
                $this->error('头像不能为空');
            }
            if(empty($post['password'])){
                $this->error('密码不能为空');
            }
            $label_id = implode(",",$post["label_id"]);
            $data = [
                'expert_group_id'=>$post['expert_group_id'],
                'service_name'=>$post['service_name'],
                'label_id'=>$label_id,
                'service_img'=>$post['image'],
                'lun_img'=>$post['logo'],
                'phone'=>$post['phone'],
                'sex'=>$post['sex'],
                'age'=>$post['age'],
                'password'=>md5($post['password']),
                'proficient_in'=>$post['proficient_in'],
                'brief_introduction'=>$post['brief_introduction']
            ];
            try {
               $this->model->where('id',$post['id'])->update($data);
            } catch (\Exception $e) {
                $this->error('保存失败手机号不能重复');
                //$this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功') ;
        }else{
            $post = $this->request->param(); 
            $row = $this->model->where('id',$post['id'])->find();
            $expert_group =  Db::name("expert_group")->order("id desc")->select();
            $this->assign("expert_group",$expert_group);
            $this->assign('row', $row);
            $label_nan = Db::name("label")->where("sex",1)->select();
            $label_nv = Db::name("label")->where("sex",2)->select();
            $this->assign("label_nan",$label_nan);
            $this->assign("label_nv",$label_nv);

            $row["label_id"] = explode(",",$row["label_id"]);

        }
        return $this->fetch();
    }

    
    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $row = Db::name("service_technician")->where('id', $id)->find();
        empty($row) && $this->error('数据不存在');
        if($row["status"] == 1){
            $this->error('已上线不能删除');
        }
        try {
            //$save = $row->delete();
            $save = Db::name("service_technician")->where("id",$id)->delete();
            if($row["uid"] > 0){
                Db::name("user")->where("id",$row["uid"])->upadte(["service_technician_id"=>0]);
            }
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

    //技师选择项目上线 
    public function golive()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post["service_goods_id"])){
                $this->error("项目不能为空");
            }
            $service_goods_id = implode(",",$post["service_goods_id"]);

            $this->model->where("id",$post["id"])->update(["status"=>1,"service_goods_id"=>$service_goods_id.',']);
            $this->success('操作成功');
        }else{
            $post = $this->request->param();
            $row = $this->model->where('id',$post['id'])->find();
            $row->service_goods_id = explode(',', $row->service_goods_id); 
            $service_goods = Db::name("service_goods")->order("id desc")->column('title', 'id');
            $this->assign('service_goods', $service_goods);
            $this->assign('row', $row);
            return $this->fetch();
        }
        
    }

    //技师下线
    public function Offline(){
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $this->model->where('id', $post['id'])->update(['status' =>0]);
            $this->success('操作成功', '');
        }
    }

    //离线
    public function off_line_url(){
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $this->model->where("id",$post["id"])->update(["status_type"=>0]);
            $this->success('操作成功');
        }
    }

     //忙碌
     public function busy_url(){
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $this->model->where("id",$post["id"])->update(["status_type"=>2]);
            $this->success('操作成功');
        }
    }

     //上线
     public function golive_url(){
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $this->model->where("id",$post["id"])->update(["status_type"=>1]);
            $this->success('操作成功');
        }
    }

   



    


 
    
 
} 